<?php

/**
 * 产品规则属性库存表 "{{pro_attribute}}".
 *
 * The followings are the available columns in table '{{pro_attribute}}':
 * @property integer $zpa_id     ID
 * @property integer $zp_id      产品ID
 * @property integer $zpa_type   类型:1属性2规格
 * @property integer $za_id      规格属性ID
 * @property string $zpa_val     值,序列化的规则值,key规则ID,value此货品所具有的规则值
 * @property string $zpa_pro_no  货号
 * @property string $zpa_pic     规格图片
 * @property integer $zpa_stock  库存
 * @property integer $zpa_weight 重量
 * @property integer $zps_photo  是否图片显示:1否2是
 * @property string $zps_md5     规格的md5值
 */
class ProAttribute extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ProAttribute the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pro_attribute}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zp_id, zpa_type, za_id, zpa_stock, zpa_weight, zps_photo', 'numerical', 'integerOnly'=>true),
			array('zpa_type','in','range'=>array('1','2','3')),
			array('zps_photo','in','range'=>array('1','2')),
			array('zpa_pro_no, zps_md5, za_title', 'length', 'max'=>50),
			array('zpa_val,zpa_pic', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpa_id, zp_id, zpa_type, za_id, zpa_val, zpa_pro_no, zpa_pic, zpa_stock, zpa_weight, zps_photo, zps_md5', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(	
				'att'=>array(self::BELONGS_TO,'Attribute','za_id','on'=>'za_type=1' ),//取属性
				'spec'=>array(self::BELONGS_TO,'Attribute','za_id','on'=>'za_type=2' ),//取默认规格
				'attribute'=>array(self::BELONGS_TO,'Attribute','za_id'),//规格
				'price'=>array(self::HAS_ONE,'ProSpe','zpa_id','select'=>'zps_price' ),//取规格价格
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpa_id' => 'ID',
			'zp_id' => '产品ID',
			'zpa_type' => '类型',
			'za_id' => '规格/属性ID',
			'za_title' => '规格/属性名称',
			'zpa_val' => '值',
			'zpa_pro_no' => '货号',
			'zpa_pic' => '规则图片',
			'zpa_stock' => '库存',
			'zpa_weight' => '重量',
			'zps_photo' => '是否图片显示',
			'zps_md5' => 'Md5值',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($pagesize=100)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpa_id',$this->zpa_id);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zpa_type',$this->zpa_type);
		$criteria->compare('za_id',$this->za_id);
		$criteria->compare('za_title',$this->za_title);
		$criteria->compare('zpa_val',$this->zpa_val,true);
		$criteria->compare('zpa_pro_no',$this->zpa_pro_no,true);
		$criteria->compare('zpa_pic',$this->zpa_pic,true);
		$criteria->compare('zpa_stock',$this->zpa_stock);
		$criteria->compare('zpa_weight',$this->zpa_weight);
		$criteria->compare('zps_photo',$this->zps_photo);
		$criteria->compare('zps_md5',$this->zps_md5,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>$pagesize)
		));
	}

	/**
	 * 增加规格组记录
	 * @return 
	 */
	public function create()
	{
		
	}

	/**
	 * 删除规格组记录
	 * @return 
	 */
	public function remove()
	{
		
	}

	/**
	 * 更新规格组库存
	 * @return 
	 */
	public function updateStock($zpaId,$amount)
	{
		$this->updateCounters($amount,'zpa_id=:id',array(':id'=>$zpaId));
	}

	/**
	 * 批量更新规格组库存
	 * @params array $amounts 库存数组,下标为产品ID，
	 * @return 
	 */
	public function updateStocks($amounts)
	{
		$pro_ids = implode(',',array_keys($amounts));
		$sql = "select ps.*,p.id as pid,p.zp_name from zd_product p left join zd_pro_spe ps on p.id=ps.zp_id where ps.zp_id in($pro_ids) order by ps.zps_price asc";
		$result = Yii::app()->db->createCommand($sql)->queryAll();
		foreach($result as $value){
			$this->updateCounters($value,'zpa_id=:id',array(':id'=>$key));
		}
	}
	
	protected function afterSave(){
		if(($this->zpa_type==1||$this->zpa_type==2)&&empty($this->za_title)){
			$model = $this->findByPk($this->zpa_id);
			$att = Attribute::model()->findByPk($this->za_id);
			$model->za_title = $att->za_title;
			$model->save();
		}
		//清空缓存
		$this->DelCatcache();
		return true;
	}
	
	public function DelCatcache(){
		Yii::app()->filecache->delete('mark_val');
	}
	
	function afterDelete(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	
	
}